<?php
/**
 * XIE--Scorpion
 *
 * 前台展示
 *
 * time 2016-5-13
 */
namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB,Input,Redirect,Session,url;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Home\User;
header('content-type:text/html;charset=utf-8');

class IndexController extends Controller
{
    //主页面
    public function index()
    {
        //切换城市
        $cs_qh=DB::table('config')->where('config_name','city')->get();
        $city_id=$cs_qh[0]->config_values;
        $cs_qh2=DB::table('city')->where('city_id',$city_id)->get();
        $city_name=$cs_qh2[0]->city_name;
        //切换城市
        $cs=DB::table('city')->get();

        //登录状态
        $user_name = Session::get('user_name');

        //友情链接
        $blogroll = DB::table('friendshiplink')->orderBy('friendlink_sort', 'desc')->get();

        //轮播图  广告位
        $carousel = DB::table('slider_img')->join('slider_type', 'slider_type.slider_type_id', '=', 'slider_img.slider_type_id')->orderBy('hot', 'desc')->limit(12)->where('slider_click','1')->get();

       //无限接分类 调用Model层,用于处理左侧分类
        $position_type=$this->getsun();

        //热词搜索
        $hot_search = DB::table('search')->orderBy('number', 'desc')->take(8)->get();

        //入库查询搜索的值对应的数据
        //热门职位 权限 1
        if($city_id==9){
            $position_one=DB::table('post_message')->where('jurisdiction',1)->join('position','post_message.position_id','=','position.position_id')->join('enterprise','post_message.enterprise_id','=','enterprise.enterprise_id')->join('city','post_message.city_id','=','city.city_id')->join('pay','post_message.pay_id','=','pay.pay_id')->join('suffer','post_message.suffer_id','=','suffer.suffer_id')->join('background','post_message.background_id','=','background.background_id')->get();
            //最新职位 权限 0
            $position_two=DB::table('post_message')->where('jurisdiction',0)->join('position','post_message.position_id','=','position.position_id')->join('enterprise','post_message.enterprise_id','=','enterprise.enterprise_id')->join('city','post_message.city_id','=','city.city_id')->join('pay','post_message.pay_id','=','pay.pay_id')->join('suffer','post_message.suffer_id','=','suffer.suffer_id')->join('background','post_message.background_id','=','background.background_id')->get();
        }else{
            $position_one=DB::table('post_message')->where('post_message.city_id',$city_id)->where('jurisdiction',1)->join('position','post_message.position_id','=','position.position_id')->join('enterprise','post_message.enterprise_id','=','enterprise.enterprise_id')->join('city','post_message.city_id','=','city.city_id')->join('pay','post_message.pay_id','=','pay.pay_id')->join('suffer','post_message.suffer_id','=','suffer.suffer_id')->join('background','post_message.background_id','=','background.background_id')->get();
            //最新职位 权限 0
            $position_two=DB::table('post_message')->where('post_message.city_id',$city_id)->where('jurisdiction',0)->join('position','post_message.position_id','=','position.position_id')->join('enterprise','post_message.enterprise_id','=','enterprise.enterprise_id')->join('city','post_message.city_id','=','city.city_id')->join('pay','post_message.pay_id','=','pay.pay_id')->join('suffer','post_message.suffer_id','=','suffer.suffer_id')->join('background','post_message.background_id','=','background.background_id')->get();
        }

        //print_r($position_one);die;

     return view('homes/index',compact('cs', 'city_name', 'cs_qh', 'user_name', 'blogroll', 'carousel', 'position_type', 'position_one', 'position_two', 'hot_search'));

    }

    //切换城市
    public function qhcs_qhcs()
    {
        $city_id=isset($_REQUEST['city_id'])?$_REQUEST['city_id']:1;
        $cs=DB::table('config')->where("config_name",'city')->update(array('config_values'=>$city_id));
        echo $cs;
    }

    //登录页面
    public function login()
    {
    	return view('homes/login');
    }

    //注册页面
    public function register()
    {
    	return view('homes/register');
    }







    /*-------------------无限极 分类--------- start -----------------*/

    public function getcates()
    {
        $data = DB::table("position")->get();
        return $this->nolist($data, 0, 0);
    }

    public function nolist($data, $parent_id = 0, $level = 0)
    {
        static $arr = array();
        foreach ($data as $k => $v) {
            if ($v->position_parent == $parent_id) {
                $v->level = $level;
                $arr[] = $v;
                //求出所有的子分类
                $this->nolist($data, $v->position_id, $level + 1);
            }
        }
        return $arr;
    }

    //调用子孙操作
    public function getsun()
    {
        $data = DB::table("position")->orderBy('position_hot', 'desc')->get();
        return $this->digui($data, 0);
    }

    //调用递归完成取出所有数据，形成子孙节点的架构
    public function digui($data, $parent_id = 0)
    {
        $child = array();
        foreach ($data as $key => $v) {
            if ($v->position_parent == $parent_id) {
                $child[] = $v;
            }
        }
        if (empty($child)) {
            return null;
        }
        //取出子级
        foreach ($child as $key => $v) {
            $current_child = $this->digui($data, $v->position_id);
            if ($current_child) {
                $child[$key]->child = $current_child;
            }
        }
        return $child;
    }

    /*-------------------无限极 分类--------- end -----------------*/



}




?>